package com.aigc.sclassroom.controller;

import com.aigc.sclassroom.pojo.Course;
import com.aigc.sclassroom.pojo.Result;
import com.aigc.sclassroom.pojo.Team;

import com.aigc.sclassroom.pojo.upStudent;
import com.aigc.sclassroom.service.CourseService;
import com.aigc.sclassroom.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    //根据教师id获取课程列表
    @GetMapping

    public Result<List<Course>> list(){
        List<Course> cs = courseService.list();

        return Result.success(cs);
    }
    //获取课程详情
    @GetMapping("/detail")
    public Result<Course> getCourseDetail(Long courseManageId){
        Course course = courseService.getCourseDetail(courseManageId);
        return Result.success(course);
    }

    //根据课程管理id查询该课程学生名单
    @GetMapping("/student")
    public Result<List<Team>> listTeam(Long courseManageId){
        List<Team> ts=courseService.listTeam(courseManageId);
        return Result.success(ts);
    }


    //增加学生
    @PostMapping("/addStudent")
    public Result addStudent(Long userId,Long courseManageId){
        courseService.addStudent(userId,courseManageId);
        return Result.success();
    }

    //删除学生
    @DeleteMapping("/deStudent")
    public Result deleteStudent(Long teamId){
        courseService.deleteStudent(teamId);
        return Result.success();
    }

    //导入学生名单
    @PostMapping("/uploadStudent")
    public Result uploadStreetTownFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("上传的文件为空");
        }

        try (InputStream inputStream = file.getInputStream()) {
            List<upStudent> students = ExcelUtils.parseExcel(inputStream);
           /* System.out.println(StreetTown + "--------------------------------");
            return Result.success("文件上传成功，并导入了 " + StreetTown.size() + " 条数据到数据库。");*/
            return courseService.uploadStudentFile(students);
        } catch (IOException e) {
            return Result.error("文件上传失败：" + e.getMessage());
        }
    }

}
